package string_question.leecode.test30;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/7 8:20
 */
public class Solution1 {

    // HashMap实现记录功能
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> res = new ArrayList<>();
        int w_nums = words.length;
        if (w_nums == 0) return res;
        int w_len = words[0].length();
        if (w_len * w_nums > s.length()) {
            return res;
        }
        HashMap<String, Integer> w_map = new HashMap<>();
        for (String word : words) {
            int count = w_map.getOrDefault(word, 0);
            w_map.put(word, count + 1);
        }
        for (int i = 0; i + (w_len * w_nums) <= s.length(); i++) {
            HashMap<String, Integer> c_map = new HashMap<>();
            int index = 0;
            while (index < w_nums) {
                String cur = s.substring(i + (w_len * index), i + (index + 1) * w_len);
                if (w_map.containsKey(cur)) {
                    int c = c_map.getOrDefault(cur, 0);
                    c_map.put(cur, c + 1);
                    if (c + 1 > w_map.get(cur)) {
                        break;
                    }
                } else {
                    break;
                }
                index++;
            }
            if (index == w_nums) {
                res.add(i);
            }
        }
        return res;
    }
}
